﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Library
{
    class DalDonDatHang
    {
        internal static List<DonDatHang> GetList()
        {
            List<DonDatHang> result = new List<DonDatHang>();

            QLDCTTEntities da = new QLDCTTEntities();

            try
            {
                result = da.DonDatHang.ToList();
            }

            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

        internal static List<DonDatHang> GetListByNgay(DateTime fromDate, DateTime toDate, List<DonDatHang> lst)
        {
            List<DonDatHang> result = new List<DonDatHang>();

            QLDCTTEntities da = new QLDCTTEntities();

            try
            {
                result = (from dh in lst
                         where dh.Ngay >= fromDate && dh.Ngay <= toDate
                         select dh).ToList();
            }

            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

        internal static void Update(DonDatHang bizdh)
        {
            try
            {
                QLDCTTEntities entities = new QLDCTTEntities();
                DonDatHang sp = entities.DonDatHang.Single(p => p.MaDDH == bizdh.MaDDH);

                sp.TrangThaiGiaoHang = bizdh.TrangThaiGiaoHang;

                entities.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        internal static void Delete(DonDatHang bizdh)
        {
            try
            {
                QLDCTTEntities entities = new QLDCTTEntities();
                DonDatHang dh = entities.DonDatHang.Single(p => p.MaDDH == bizdh.MaDDH);

                entities.DonDatHang.Remove(dh);

                entities.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
